﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang.DanhMuc
{
    public partial class F400_Danh_muc_khach_hang : Form
    {
        public F400_Danh_muc_khach_hang()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private bool check_validate_is_ok()
        {
            if (string.IsNullOrWhiteSpace(m_txt_ma_khach_hang.Text))
            {
                MessageBox.Show("Bạn phải nhập mã khách hàng!", "Thông báo");
                m_txt_ma_khach_hang.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(m_txt_ten_khach_hang.Text))
            {
                MessageBox.Show("Bạn phải nhập tên khách hàng!", "Thông báo");
                m_txt_ten_khach_hang.Focus();
                return false;
            }
            return true;
        }

        private void set_define_events()
        {
            this.Load += F400_Danh_muc_khach_hang_Load;
            this.m_cmd_them_khach_hang.Click +=m_cmd_them_khach_hang_Click;
            this.m_cmd_cap_nhat_tt_khach_hang.Click += m_cmd_cap_nhat_tt_khach_hang_Click;
            this.m_cmd_quay_lai.Click += m_cmd_quay_lai_Click;
            this.m_cmd_tim_kiem_khach_hang.Click += m_cmd_tim_kiem_khach_hang_Click;
            this.m_cmd_xoa_khach_hang.Click += m_cmd_xoa_khach_hang_Click;
            this.m_cmd_xoa_trang.Click += m_cmd_xoa_trang_Click;
            this.m_txt_tim_kiem_khach_hang.TextChanged += m_txt_tim_kiem_khach_hang_TextChanged;
            this.m_grv_khach_hang.Click += m_grv_khach_hang_Click;

            //this.m_cmd_them_khach_hang.Click += new System.EventHandler(this.m_cmd_them_khach_hang_Click);
            //this.m_cmd_cap_nhat_tt_khach_hang.Click += new System.EventHandler(this.m_cmd_cap_nhat_tt_khach_hang_Click);
            //this.m_cmd_xoa_trang.Click += new System.EventHandler(this.m_cmd_xoa_trang_Click);
            //this.m_txt_tim_kiem_khach_hang.TextChanged += new System.EventHandler(this.m_txt_tim_kiem_khach_hang_TextChanged);
            //this.m_cmd_xoa_khach_hang.Click += new System.EventHandler(this.m_cmd_xoa_khach_hang_Click);
            //this.m_cmd_tim_kiem_khach_hang.Click += new System.EventHandler(this.m_cmd_tim_kiem_khach_hang_Click);
            //this.m_cmd_quay_lai.Click += new System.EventHandler(this.m_cmd_quay_lai_Click);
        }

        private void load_data_to_grid()
        {
            var v_lst_khach_hang = db.DM_KHACH_HANG
                .Where(x => x.MA_KHACH_HANG.Contains(m_txt_tim_kiem_khach_hang.Text.Trim())
                || x.TEN_KHACH_HANG.Contains(m_txt_tim_kiem_khach_hang.Text.Trim())
                || m_txt_tim_kiem_khach_hang.Text == "")
                .Select(x => new
                {
                    ID = x.ID,
                    MA_KHACH_HANG = x.MA_KHACH_HANG,
                    TEN_KHACH_HANG = x.TEN_KHACH_HANG,
                    DIA_CHI = x.DIA_CHI,
                    DIEN_THOAI = x.DIEN_THOAI
                })
                .OrderBy(x => x.MA_KHACH_HANG)
                .ToList();
            m_grv_khach_hang.DataSource = v_lst_khach_hang;

            m_grv_khach_hang.Columns[0].HeaderText = "ID";
            m_grv_khach_hang.Columns[0].Visible = false;
            m_grv_khach_hang.Columns[1].HeaderText = "Mã KH";
            m_grv_khach_hang.Columns[1].Width = 70;
            m_grv_khach_hang.Columns[2].HeaderText = "Tên KH";
            m_grv_khach_hang.Columns[2].Width = 120;
            m_grv_khach_hang.Columns[3].HeaderText = "Địa chỉ";
            m_grv_khach_hang.Columns[3].Width = 200;
            m_grv_khach_hang.Columns[4].HeaderText = "SĐT";
            m_grv_khach_hang.Columns[4].Width = 40;
            m_grv_khach_hang.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        #endregion

        #region Events

        private void F400_Danh_muc_khach_hang_Load(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_txt_tim_kiem_khach_hang_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        void m_grv_khach_hang_Click(object sender, EventArgs e)
        {
            try
            {
                decimal id_khach_hang = (decimal)m_grv_khach_hang.CurrentRow.Cells[0].Value;
                DM_KHACH_HANG v_dm_khach_hang = db.DM_KHACH_HANG.FirstOrDefault(x => x.ID == id_khach_hang);
                if (v_dm_khach_hang != null)
                {
                    m_txt_ma_khach_hang.Text = v_dm_khach_hang.MA_KHACH_HANG;
                    m_txt_ten_khach_hang.Text = v_dm_khach_hang.TEN_KHACH_HANG;
                    m_txt_dia_chi_khach_hang.Text = v_dm_khach_hang.DIA_CHI;
                    m_txt_dien_thoai_khach_hang.Text = v_dm_khach_hang.DIEN_THOAI.ToString();
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_them_khach_hang_Click(object sender, EventArgs e)
        {
            try
            {
                if (check_validate_is_ok())
                {
                    DM_KHACH_HANG v_dm_khach_hang = new DM_KHACH_HANG();
                    v_dm_khach_hang.MA_KHACH_HANG = m_txt_ma_khach_hang.Text.Trim();
                    v_dm_khach_hang.TEN_KHACH_HANG = m_txt_ten_khach_hang.Text.Trim();
                    v_dm_khach_hang.DIA_CHI = m_txt_dia_chi_khach_hang.Text.Trim();
                    v_dm_khach_hang.DIEN_THOAI = Convert.ToDecimal(m_txt_dien_thoai_khach_hang.Text.Trim());
                    db.DM_KHACH_HANG.Add(v_dm_khach_hang);
                    m_cmd_xoa_trang_Click(null, null);
                    db.SaveChanges();
                    MessageBox.Show("Bạn đã thêm thành công khách hàng!", "Thông báo!");
                    load_data_to_grid();

                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_cap_nhat_tt_khach_hang_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_grv_khach_hang.CurrentRow == null)
                    return;
                decimal id_khach_hang = (decimal)m_grv_khach_hang.CurrentRow.Cells[0].Value;
                var v_dm_khach_hang = db.DM_KHACH_HANG.FirstOrDefault(x => x.ID == id_khach_hang);
                if (v_dm_khach_hang != null)
                {
                    if (check_validate_is_ok())
                    {
                        v_dm_khach_hang.MA_KHACH_HANG = m_txt_ma_khach_hang.Text.Trim();
                        v_dm_khach_hang.TEN_KHACH_HANG = m_txt_ten_khach_hang.Text.Trim();
                        v_dm_khach_hang.DIA_CHI = m_txt_dia_chi_khach_hang.Text.Trim();
                        v_dm_khach_hang.DIEN_THOAI = Convert.ToDecimal(m_txt_dien_thoai_khach_hang.Text.Trim());
                        db.SaveChanges();
                        m_cmd_xoa_trang_Click(null, null);
                        MessageBox.Show("Cập nhật thành công thông tin khách hàng!", "Thông báo!");
                        load_data_to_grid();
                    }
                }
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_trang_Click(object sender, EventArgs e)
        {
            try
            {
                m_txt_ma_khach_hang.Text = "";
                m_txt_ten_khach_hang.Text = "";
                m_txt_dia_chi_khach_hang.Text = "";
                m_txt_dien_thoai_khach_hang.Text = "";
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_tim_kiem_khach_hang_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xoa_khach_hang_Click(object sender, EventArgs e)
        {
            if (m_grv_khach_hang.CurrentRow == null)
            {
                MessageBox.Show("Bạn hãy chọn khách hàng để xóa!", "Thông báo!");
                return;
            }
            decimal id_khach_hang = (decimal)m_grv_khach_hang.CurrentRow.Cells[0].Value;
            var v_dm_khach_hang = db.DM_KHACH_HANG.FirstOrDefault(x => x.ID == id_khach_hang);
            if (v_dm_khach_hang != null)
            {
                if (MessageBox.Show("Bạn có chắc chắn muốn KHÁCH HÀNG này và các THÔNG TIN liên quan không?", "Cảnh báo!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //Xóa dữ liệu bảng GD_HOA_DON_BAN
                    var v_lst_hoa_don = db.GD_HOA_DON_BAN
                        .Where(x => x.ID_KHACH_HANG == id_khach_hang)
                        .ToList();
                    for (int i = 0; i < v_lst_hoa_don.Count; i++)
                    {
                        db.GD_HOA_DON_BAN.Remove(v_lst_hoa_don[i]);
                        db.SaveChanges();
                    }
                    //Xóa dữ liệu bảng DM_KHACH_HANG
                    db.DM_KHACH_HANG.Remove(v_dm_khach_hang);
                    db.SaveChanges();
                    MessageBox.Show("Bạn đã xóa thành công khách hàng!", "Thông báo!");
                    load_data_to_grid();

                }
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }
        #endregion
    }
}
